<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_user_shipping_address extends user_shipping_address {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($usa_id = '') {
        global $_G;
        $detail = array();
        $usa_id = isset($_REQUEST['usa_id']) ? $_REQUEST['usa_id'] : '';
        $detail = $this->one_info('user_shipping_address', 'user_id', $_G['user_id'],'usa_isdefault',1);
        //DEBUG 调试输出
        if ($_G['gp_api']) {
            if ($detail) {
                $return['code'] = '0';
                unset($detail["usa_lat"]);
                unset($detail["usa_lng"]);
                unset($detail["usa_isdefault"]);
                unset($detail["modify_dateline"]);
                unset($detail["isdelete"]);
                //DEBUG 补充地区名称名称
                $am_ids_name = $this->get_field_value_by_id('area_main', 'am_id', array($detail['am_id_1'], $detail['am_id_2'], $detail['am_id_3']), 'am_name');
                $detail['am_id_1_name'] = $am_ids_name[$detail['am_id_1']];
                $detail['am_id_2_name'] = $am_ids_name[$detail['am_id_2']];
                $detail['am_id_3_name'] = $am_ids_name[$detail['am_id_3']];
                $return['data'] = $detail;
            } else {
                //DEBUG 如果用户没有地址，返回前端一个空地址
                $detail = array(
                    "usa_user_realname"=>"",//收货人姓名
                    "user_id"=>"",//地址归属用户编号
                    "usa_user_phone"=>"",//收货认电话
                    "usa_address"=>"",//收货地址
                    "create_dateline"=>"",//创建时间戳
                    "am_id_1"=>"",//省编号
                    "am_id_2"=>"",//市编号
                    "am_id_3"=>"",//县编号
                    "am_id_1_name"=>"",//省编号名称
                    "am_id_2_name"=>"",//市编号名称
                    "am_id_3_name"=>""//县编号名称
                );
                $return['data'] = $detail;
                unset($detail["create_dateline"]);
                $return['code'] = '0';
            }
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }else{
            //include template('user/shipping_address/detail');   
        }
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $user_shipping_address = isset($_REQUEST['user_shipping_address']) && !empty($_REQUEST['user_shipping_address']) ? $_REQUEST['user_shipping_address'] : '';
        if ('1' == $issubmit || $_G['gp_api']) {
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach ($user_shipping_address AS $key => $value) {
                if (is_array($value) && !empty($value)) {
                    $user_shipping_address[$key] = implode(",", $value);
                }
            }
            $user_shipping_address['user_id'] = $_G['user_id'];
            $user_shipping_address['create_dateline'] = TIMESTAMP;
            if ($user_shipping_address['usa_isdefault'] == 1) {
                //把之前用户的其他地址设置为非默认地址
                DB::update('user_shipping_address', array('usa_isdefault' => '0'), array('user_id' => $_G['user_id']));
            }
            $insert_id = $this->add('user_shipping_address', $user_shipping_address);
            if ($_G['gp_api']) {
                if ($insert_id) {
                    $return['code'] = '0';
                    $user_shipping_address['usa_id'] = $insert_id;
                    $user_shipping_address['create_dateline'] = date('Y-m-d H:i:s');
                    $return['data']['user_shipping_address'] = $user_shipping_address;
                } else {
                    $return['code'] = '1';
                }
                echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
            } else {
                if ($insert_id) {
                    showmessage('操作成功', 'index.php?mod=user&action=shipping_address&do=index');
                } else {
                    showmessage('操作失败', 'index.php?mod=user&action=shipping_address&do=index');
                }
            }
        } else {
            include template('user/shipping_address/add');
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
         @file_put_contents(SITE_ROOT.'./data/debug/input_log.txt','RECEIVE EDIT: '.date('Y-m-d H:i:s').' '.json_encode($_REQUEST).PHP_EOL,FILE_APPEND);
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $user_shipping_address = isset($_REQUEST['user_shipping_address']) && !empty($_REQUEST['user_shipping_address']) ? $_REQUEST['user_shipping_address'] : '';
        $usa_id = $user_shipping_address['usa_id'];
        if(empty($usa_id)){
            //DEBUG 取出用户默认 地址编号进行更新 如果用户没有 $usa_id 则插入地址记录
            $user_shipping_address_detail = $this->one_info('user_shipping_address', 'user_id', $_G['user_id'],'usa_isdefault',1);
            if($user_shipping_address_detail['usa_id']){
                $usa_id=$user_shipping_address_detail['usa_id'];
            }
        }
        //if ($usa_id) {
        if (1) {
            if ('1' == $issubmit || $_G['gp_api']) {
                $user_shipping_address['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach ($user_shipping_address AS $key => $value) {
                    if (is_array($value) && !empty($value)) {
                        $user_shipping_address[$key] = implode(",", $value);
                    }
                }
                if ($user_shipping_address['usa_isdefault'] == 1) {
                    //把之前用户的其他地址设置为非默认地址
                    DB::update('user_shipping_address', array('usa_isdefault' => '0'), array('user_id' => $_G['user_id']));
                }
                if ($usa_id) {
                    $where = array('usa_id' => $usa_id);
                    $user_shipping_address['modify_dateline'] = TIMESTAMP;
                    $effect_row = $this->edit('user_shipping_address', $user_shipping_address, $where);                    
                }else{
                    $user_shipping_address['user_id'] =$_G['user_id'];
                    $user_shipping_address['usa_isdefault'] = 1;
                    $user_shipping_address['create_dateline'] = TIMESTAMP;
                    $insert_id = $this->add('user_shipping_address', $user_shipping_address);
                }
                if ($_G['gp_api'] && $_G['user_id']) {
                    $return['code'] = '0';
                    $return['data']['user_shipping_address'] = $user_shipping_address;
                    echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
                } else {
                    if ($effect_row) {
                        showmessage('操作成功', 'index.php?mod=user&action=shipping_address&do=index');
                    } else {
                        showmessage('数据未有变化或操作异常,请重新操作', 'index.php?mod=user&action=shipping_address&do=index');
                    }
                }
            } else {
                //DEBUG 获取操作对象信息
                $info = $this->one_info('user_shipping_address', 'usa_id', $usa_id);
                include template('user/shipping_address/edit');
            }
        } else {
            showmessage('请选择编辑对象', 'index.php?mod=user&action=shipping_address&do=index');
        }
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        $usa_id = isset($_REQUEST['usa_id']) ? $_REQUEST['usa_id'] : '';
        if ($usa_id) {
            $effect_row = $this->delete("user_shipping_address", array("usa_id" => $usa_id), $limit = 1);
            if ($effect_row) {
                showmessage('操作成功', 'index.php?mod=user&action=shipping_address&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=user&action=shipping_address&do=index');
            }
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $wheresql = " AND user_id='" . $_G['user_id'] . "' ";
        if ($keyword) {
            $wheresql = " AND usa_user_realname LIKE '%" . $keyword . "%' ";
        }
        $orderby = " ORDER BY usa_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('user_shipping_address', $page_condition);
        foreach ($page_result['page_data'] AS $key => $value) {
            $am_ids_name = $this->get_field_value_by_id('area_main', 'am_id', array($value['am_id_1'], $value['am_id_2'], $value['am_id_3']), 'am_name');
            $value['am_id_1_name'] = $am_ids_name[$value['am_id_1']];
            $value['am_id_2_name'] = $am_ids_name[$value['am_id_2']];
            $value['am_id_3_name'] = $am_ids_name[$value['am_id_3']];
            $page_result['page_data'][$key] = $value;
        }
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $return['data']['course_main_index'] = $page_result;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=shipping_address&do=index&keyword=" . $keyword);
            include template('user/shipping_address/index');
        }
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY usa_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('user_shipping_address', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>